<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>职位管理</title>
    <link href="../../../static/css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../../../static/js/jquery.js"></script>
    <script type="text/javascript">
        function tipOpen(jobId,jobState) {
            $.ajax({
                type: "post",
                url: "/job/updateJobState",
                data: {jobId:jobId,jobState:jobState},
                dataType: "JSON",
                success: function () {
                    window.location.href = "findByPage";
                },
                error: function (rm) {
                    alert(rm.msg);
                }
            });
        }

        function tipClose() {
            $("#tip").fadeOut(200);
        }

        $(function () {
            var jobNamelike = $("#jobNamelike").val();
            var jobDeptId = $("#jobDeptId").val();
            var pageNum = 1;

            // URL解码
            if (jobDeptId) {
                jobDeptId = window.decodeURIComponent(jobDeptId);
            }
            if (jobNamelike) {
                jobNamelike = window.decodeURIComponent(jobNamelike);
            }

            //部门查询
            $.ajax({
                type: "post",
                url: "/dept/deptList",
                data: {},
                dataType: "JSON",
                success: function (rm) {
                    var deptstr = '<option value="">请选择</option>';
                    for (let i = 0; i < rm.data.length; i++) {
                        deptstr += '<option value="' + rm.data[i].deptId + '">' + rm.data[i].deptName + '</option>\n';
                    }
                    $("#jobDeptId").html(deptstr);
                },
                error: function (rm) {
                    alert(rm.msg)
                }
            });

            load(pageNum, jobNamelike, jobDeptId);
        })

        function load(pageNum, jobNamelike, jobDeptId) {
            $.ajax({
                type: "post",
                url: "/job/pageJob",
                data: {pageNum: pageNum, jobNamelike: jobNamelike, jobDeptId: jobDeptId},
                dataType: "JSON",
                async: true,
                success: function (rm) {

                    let list = rm.data.data;
                    let str = '<thead>\n' +
                        '            <tr>\n' +
                        '                <th>序号</th>\n' +
                        '                <th>职位</th>\n' +
                        '                <th>所属部门</th>\n' +
                        '                <th>状态</th>\n' +
                        '                <th>操作</th>\n' +
                        '            </tr>\n' +
                        '            </thead>\n' +
                        '  <tbody>';
                    for (let i = 0; i < list.length; i++) {
                        let status;
                        if (list[i].jobState == 1) {
                            status = "正常";
                        } else if (list[i].jobState == 2) {
                            status = "已撤销";
                        } else {
                            status = "";
                        }

                        str += '            <tr>\n' +
                            '                <td>' + (i + 1) + '</td>\n' +
                            '                <td>' + list[i].jobName + '</td>\n' +
                            '                <td>' + list[i].deptName + '</td>\n' +
                            '                <td>' + status + '</td>\n' +
                            '                <td>\n' +
                            '                    <a href="positionUpdate" onclick="jobUpdate(' + list[i].jobId + ')" class="tablelink">修改</a>\n' +
                            '                    <a href="positionGrant" onclick="fuquan(' + list[i].jobId + ')" class="tablelink">赋权</a>\n';
                        if (list[i].jobState == 1) {
                            str += '<a href="javascript:void(0)" class="tablelink" onclick="tipOpen(' + list[i].jobId + ',' + list[i].jobState + ')">注销</a>\n';
                        } else {
                            str += '<a href="javascript:void(0)" class="tablelink" onclick="tipOpen(' + list[i].jobId + ',' + list[i].jobState + ')">恢复</a>\n';
                        }
                        str += '                </td>\n' +
                            '            </tr>';
                    }
                    str += "  </tbody>";
                    $("#jobList").html(str);

                    $("#totalCount").html(rm.data.totalCount);
                    $("#pageNum").html(rm.data.pageNum);

                    let begin = rm.data.pageNum - 5;
                    let end = rm.data.pageNum + 4;
                    if (begin < 1) {
                        begin = 1;
                        if (rm.data.totalPage < 10) {
                            end = rm.data.totalPage;
                        } else {
                            end = 10;
                        }
                    }

                    if (end > rm.data.totalPage) {
                        end = rm.data.totalPage;
                        if (begin < 0) {
                            begin = 1;
                        } else {
                            if (rm.data.totalPage > 10) {
                                begin = rm.data.totalPage - 9;
                            } else {
                                begin = 1;
                            }
                        }
                    }


                    let index = '<li class="paginItem" onclick="javascript:load(1,\'' + jobNamelike + '\',\'' + jobDeptId + '\')"><a href="javascript:void(0)">首页</a></li>';
                    let beforeNum = rm.data.pageNum - 1;
                    if (beforeNum < 1) {
                        beforeNum = 1;
                    }
                    index += '<li class="paginItem" onclick="javascript:load(' + beforeNum + ',\'' + jobNamelike + '\',\'' + jobDeptId + '\')"><a href="javascript:void(0)"><span class="pagepre"></span></a></li>';
                    for (let i = begin; i <= end; i++) {
                        if (i == rm.data.pageNum) {
                            index += '<li class="paginItem current" onclick="javascript:load(' + i + ',\'' + jobNamelike + '\',\'' + jobDeptId + '\')"><a href="javascript:void(0)">' + i + '</a></li>';
                        } else {
                            index += '<li class="paginItem" onclick="javascript:load(' + i + ',\'' + jobNamelike + '\',\'' + jobDeptId + '\')" ><a href="javascript:void(0)">' + i + '</a></li>';
                        }
                    }
                    let nextNum = rm.data.pageNum + 1;
                    if (nextNum >= rm.data.totalPage) {
                        nextNum = rm.data.totalPage;
                    }
                    index += '<li class="paginItem" onclick="javascript:load(' + nextNum + ',\'' + jobNamelike + '\',\'' + jobDeptId + '\')"><a href="javascript:void(0)"><span class="pagenxt"></span></a></li>';
                    index += '<li class="paginItem" onclick="javascript:load(' + rm.data.totalPage + ',\'' + jobNamelike + '\',\'' + jobDeptId + '\')" ><a href="javascript:void(0)">末页</a></li>';

                    $("#paginList").html(index);
                }
                ,
                error: function (rm) {
                    alert(rm.msg)
                }
            });
        }
        //赋权传值
        function fuquan(jobId) {
            localStorage.setItem("sysjobId2", JSON.stringify(jobId));
        }

        //修改传值
        function jobUpdate(jobId) {
            localStorage.setItem("sysjobId", JSON.stringify(jobId));
        }

        //点击查询
        function PageJobList() {
            var jobNamelike = $("#jobNamelike").val();
            var jobDeptId = $("#jobDeptId").val();
            var pageNum = 1;
            load(pageNum, jobNamelike, jobDeptId);
        }
    </script>
</head>
<body>
<div class="place"><span>位置：</span>
    <ul class="placeul">
        <li><a href="../job/findByPage">系统管理</a></li>
        <li><a href="../job/findByPage">职位管理</a></li>
    </ul>
</div>
<div class="rightinfo">
    <form action="" method="post" id="Form">
        <ul class="tools">
            <li><label>职位名称:</label>
                <input type="text" id="jobNamelike" name="jobNamelike"/>
            </li>
            <li><label>所属部门:</label>
                <select name="jobDeptId" id="jobDeptId">
                    <option value="">请选择部门</option>
                    <option value="">研发</option>
                    <option value="">销售</option>
                    <option value="">财务</option>
                </select>
            </li>
            <li class="subBut" onclick="PageJobList()"><img src="../../../static/images/t06.png"/>查询</li>
            <li class="subBut" onclick="window.location.href='positionAdd'"><img src="../../../static/images/t01.png"/>添加
            </li>
        </ul>
        <table class="tablelist" id="jobList">
            <thead>
            <tr>
                <th>序号</th>
                <th>职位</th>
                <th>所属部门</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>1</td>
                <td>经理</td>
                <td>研发部</td>
                <td>正常</td>
                <td>
                    <a href="positionUpdate.html" class="tablelink">修改</a>
                    <a href="positionGrant.html" class="tablelink">赋权</a>
                    <a href="javascript:void(0)" class="tablelink" onclick="tipOpen('是否确认注销此条信息？')">注销</a>
                </td>
            </tr>
            <tr>
                <td>2</td>
                <td>副经理</td>
                <td>研发部</td>
                <td>已撤销</td>
                <td>
                    <a href="positionUpdate.html" class="tablelink">修改</a>
                    <a href="positionGrant.html" class="tablelink">赋权</a>
                    <a href="javascript:void(0)" class="tablelink" onclick="tipOpen('是否确认恢复此条信息？')">恢复</a>
                </td>
            </tr>
            <tr>
                <td>3</td>
                <td>高级工程师</td>
                <td>研发部</td>
                <td>正常</td>
                <td>
                    <a href="positionUpdate.html" class="tablelink">修改</a>
                    <a href="positionGrant.html" class="tablelink">赋权</a>
                    <a href="javascript:void(0)" class="tablelink" onclick="tipOpen('是否确认注销此条信息？')">注销</a>
                </td>
            </tr>
            </tbody>
        </table>
        <div class="pagin">
            <div class="message">共<i class="blue" id="totalCount">1256</i>条记录，当前显示第&nbsp;<i class="blue" id="pageNum">2&nbsp;</i>页
            </div>
            <ul class="paginList" id="paginList">
                <!--                <li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>-->
                <!--                <li class="paginItem"><a href="javascript:;">1</a></li>-->
                <!--                <li class="paginItem current"><a href="javascript:;">2</a></li>-->
                <!--                <li class="paginItem"><a href="javascript:;">3</a></li>-->
                <!--                <li class="paginItem"><a href="javascript:;">4</a></li>-->
                <!--                <li class="paginItem"><a href="javascript:;">5</a></li>-->
                <!--                <li class="paginItem more"><a href="javascript:;">...</a></li>-->
                <!--                <li class="paginItem"><a href="javascript:;">10</a></li>-->
                <!--                <li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>-->
            </ul>
        </div>
    </form>
    <!-- 提示框 -->
    <div id="tip" class="tip">
        <div class="tiptop"><span>提示信息</span><a onclick="tipClose()"></a></div>
        <div class="tipinfo"><span><img src="../../../static/images/ticon.png"/></span>
            <div class="tipright">
                <p></p>
                <cite>如果是请点击确定按钮 ，否则请点取消。</cite></div>
        </div>
        <div class="tipbtn">
            <input name="" type="button" class="sure" value="确定" onclick="tipClose()"/>
            &nbsp;
            <input name="" type="button" class="cancel" value="取消" onclick="tipClose()"/>
        </div>
    </div>
</div>
<script type="text/javascript">
    $('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
